package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.IareaService;
@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")
public class AreaAction implements ModelDriven<Area>{

	private Area area=new Area();
	@Override
	public Area getModel() {
		// TODO Auto-generated method stub
		return area;
	}
	
	private int page;

	private int rows;

	
	public void setPage(int page) {
		this.page = page;
	}


	public void setRows(int rows) {
		this.rows = rows;
	}
	
	@Autowired
	private IareaService IareaService;

	@Action(value = "area_pageQuery")
	public void area_pageQuery(){
		Pageable pageable = new PageRequest(page - 1, rows);
		
		Specification<Area> specification=new Specification<Area>() {
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				
				List<Predicate>list=new ArrayList<Predicate>();
				
				if(StringUtils.isNotBlank(area.getCity())){
					Predicate like1 = cb.like(root.get("city").as(String.class), "%"+area.getCity()+"%");
					list.add(like1);
				}
				if(StringUtils.isNotBlank(area.getProvince())){
					Predicate like2 = cb.like(root.get("province").as(String.class),"%"+area.getProvince()+"%" );
					list.add(like2);
				}
				if(StringUtils.isNotBlank(area.getDistrict())){
					Predicate like2 = cb.like(root.get("district").as(String.class),"%"+area.getDistrict()+"%" );
					list.add(like2);
				}
				
				
				Predicate[] array = list.toArray(new Predicate[0] );
				return cb.and(array);
				
				
			}
			
			
		};
		
		Page<Area>page=	IareaService.findAll(specification,pageable);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		
		String jsonString = JSON.toJSONString(map);
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		try {
			ServletActionContext.getResponse().getWriter().write(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	

}
